package com.econ.powercloud.ems.common.util;

import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.exception.CommonRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

/**
 * AES 加密工具类
 * @author jwc
 * @since 2023/11/23
 */
@Slf4j
public class AesUtil {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }
    /**
     * 加密算法RSA
     */
    public static final String KEY_ALGORITHM = "AES";
    /**
     * 编码方式
     */
    public static final String CODE_TYPE = "UTF-8";
    /**
     * 填充类型 AES/ECB/PKCS5Padding   AES/ECB/ISO10126Padding
     */
    public static final String AES_TYPE = "AES/ECB/PKCS7Padding";
    /**
     * AES秘钥
     */
    public static final String AES_KEY = "0pxspuixvi592263";

    /**
     * 随机生成加盐类
     */
    /*public static String getSalt(int n){
        char[] chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
                "1234567890!@#$%^&*()_+").toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        SecureRandom random = new SecureRandom();
        for(int i = 0; i < n; i++){
            stringBuilder.append(chars[random.nextInt(chars.length)]);
        }
        return stringBuilder.toString();
    }*/

    /**
     * 加密
     * @param clearText 明文
     * @return 加密串
     */
    public static String encryptAes(String clearText) {
        try {
            SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(), KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedData = cipher.doFinal(clearText.getBytes(CODE_TYPE));
            return new BASE64Encoder().encode(encryptedData);
        } catch (Exception e) {
            throw new RuntimeException("加密失败", e);
        }
    }

    /**
     * 解密
     * @param encryptText 密文
     * @return 解密串
     */
    public static String decryptAes(String encryptText) {
        try {
            byte[] byteMi = new BASE64Decoder().decodeBuffer(encryptText);
            SecretKeySpec key = new SecretKeySpec(AES_KEY.getBytes(), KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(AES_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, CODE_TYPE);
        } catch (Exception e) {
            log.warn("解密失败:{}", e);
            throw new CommonRuntimeException(StatusType.ILLEGAL_ARGUMENT, "无效的密码");
        }
    }

    /*public static void main(String[] args) {
        String json = "123456sb";
        //加密
        System.out.println("字符串：" + json);
        String encrypt = encryptAes(json);
        System.out.println("加密后字符串：" + encrypt);
        //私钥解密
        System.out.println("解密后字符串：" + decryptAes(encrypt));
    }*/
}
